<template>
  <div class="My">
    <!-- 登录 / 注册模块 -->
    <div class="header not-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 用户资料模块 -->
    <div class="header user-info" v-else>
      <div class="base-info">
        <div class="left">
          <van-image src="https://img.yzcdn.cn/vant/cat.jpeg" round fit="cover" />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round type="default" size="mini">编程资料</van-button>
        </div>
      </div>
      <div class="data-status">
        <div class="data-item">
          <div class="count">{{ userInfo.art_count }}</div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.follow_count }}</div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.fans_count }}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.like_count }}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>

    <!-- 宫格导航模块 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item>
        <i slot="icon" class="iconfont toutiaoshoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="iconfont toutiaolishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- 单元格模块 -->
    <van-cell-group class="cell-group" :border="false">
      <van-cell title="消息通知" is-link />
      <van-cell title="小爱同学" is-link />
    </van-cell-group>

    <!-- 退出模块 -->
    <van-cell-group class="out" :border="false" v-if="user">
      <van-cell title="退出登录" @click="onClickOut" clickable />
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { getUserInfo } from '@/api/user';

export default {
  data() {
    return {
      userInfo: {}, // 用户信息
    };
  },
  created() {
    if (this.user) {
      this.loadUserInfo();
    }
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    onClickOut() {
      // 在组件中需要使用 this.$dialog 来调用弹框组件
      this.$dialog
        .confirm({
          title: '确认退出吗？',
        })
        .then(() => {
          // 确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
          this.$store.commit('setUser', null);
        })
        .catch(() => {});
    },
    async loadUserInfo() {
      try {
        const { data } = await getUserInfo();
        this.userInfo = data.data;
        // console.log(data);
      } catch (err) {
        console.log(err);
        this.$toast('获取数据失败，请稍后重试');
      }
    },
  },
};
</script>

<style lang="less" scoped>
.My {
  .header {
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
    .login-btn {
      display: flex;
      flex-direction: column;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 10px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .user-info {
    .base-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 76px 32px 23px;
      height: 231px;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .van-image {
          border: 5px solid #fff;
          width: 132px;
          height: 132px;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-status {
      height: 130px;
      display: flex;
      .data-item {
        height: 100%;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    height: 141px;
    .van-grid-item {
      i {
        font-size: 45px;
      }
      .toutiaoshoucang {
        color: #eb5253;
      }
      .toutiaolishi {
        color: #ff9d1d;
      }
      span {
        font-size: 28px;
      }
    }
  }
  .cell-group {
    margin-top: 20px;
  }
  .out {
    margin-top: 8px;
    height: 50px;
    text-align: center;
    .van-cell {
      color: red;
    }
  }
}
</style>
